//
//  YLFundListController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/8/26.
//  Copyright © 2017年 she1323. All rights reserved.
//  基金列表

#import "YLFundListController.h"
#import "YLProductCollectionCell.h"
#import "YLFuncDetailTableViewController.h"
#import "YLFundListViewModel.h"
#import "YLFundListCollectionViewCell.h"
#import <MJRefresh.h>
#import "YLCertifiedPaymentViewController.h"
#import "YLAccountTool.h"

@interface YLFundListController ()

@property (nonatomic, assign) NSInteger showTag;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) YLFundListViewModel *viewModel;

@end

@implementation YLFundListController

- (YLFundListViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLFundListViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)updateData
{
    YLAccount *account = [YLAccountTool account];
    
    if (account.fundQualifications == NO) {
        return;
    }
    
    if (account.authState != 1) {
        return;
    }
    
    if (!account.isPassed) {
        return;
    }
    
    _showTag = 100;
    
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YLFundListCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:[YLFundListCollectionViewCell cellID]];
    
    __weak typeof(self) weakSelf = self;
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.index = 1;
        [weakSelf loadData];
    }];
    
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.index ++;
        [weakSelf loadData];
    }];
    
    self.collectionView.mj_footer.hidden = YES;
    [self.collectionView.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self updateData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YLFuncDetailTableViewController *detail = [[YLFuncDetailTableViewController alloc] init];
    detail.model = self.viewModel.models[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}


- (void)loadData
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:5];
    params[@"PageIndex"] = @(_index);
    params[@"MaxResultCount"] = @(10);
    params[@"ProductType"] = @(_productType);
    params[@"Duration"] = @(_duration);
    params[@"OperationMode"] = @(_operationMode);

    __weak typeof(self) weakSelf = self;
    [[self.viewModel.requestCommand execute:params] subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefresh];
        [weakSelf.collectionView reloadData];
    }];
    
    [self.viewModel.wh_loadDataErrorSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf endRefresh];
    }];
    
    [self.viewModel.wh_noMoreDataSubject subscribeNext:^(id  _Nullable x) {
        [weakSelf.collectionView.mj_footer endRefreshingWithNoMoreData];
    }];
}

- (void)endRefresh
{
    self.collectionView.mj_footer.hidden = NO;
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
}

- (void)loadNewData
{
    if (_showTag == 100) {
        // 可以加载数据
        [self.collectionView.mj_header beginRefreshing];
    } else {
        // 需要创建东西才可以加载数据
        [self updateData];
    }
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.viewModel.models.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YLFundListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[YLFundListCollectionViewCell cellID] forIndexPath:indexPath];
    cell.model = self.viewModel.models[indexPath.row];
    return cell;
}

@end
